package com.example.mypigcar.controller;

import com.example.mypigcar.common.core.SR;
import com.example.mypigcar.pojo.dto.GetCarDTO;
import com.example.mypigcar.pojo.po.Car;
import com.example.mypigcar.service.CarService;
import com.example.mypigcar.utils.JWTUtils;
import com.example.mypigcar.utils.OSSUtils;
import com.example.mypigcar.utils.UUIDUtils;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;


/**
 * @author zkw
 * @createTime 2021年07月02日 08:15:00
 */
@Api(value="车辆controller",tags={"车辆操作接口"})
@RequestMapping("car")
@RestController
public class CarController {
    @Resource
    CarService carService;

    @ApiOperation("添加车辆")
    @PostMapping("")
    public SR addCar(@ApiParam("汽车对象") @RequestBody Car car, HttpServletRequest request){
        String userIdByToken = JWTUtils.getUserIdByToken(request);
        car.setUserId(Integer.parseInt(userIdByToken));
        car.setStatus(0);
        car.setAudit(1);
        return carService.save(car)?SR.ok():SR.error();
    }

    @ApiOperation("修改车辆")
    @PutMapping("")
    public SR updateCar(@ApiParam("汽车对象") @RequestBody Car car){
        return SR.ok().data(carService.update(car));
    }

    @ApiOperation("获取全部车辆信息")
    @GetMapping("/all")
    public SR list(){
        List<Car> cars = carService.listCars();
        Collections.reverse(cars);
        return SR.ok().data(cars);
    }

    @ApiOperation("根据车牌查找车辆")
    @GetMapping("/plate/{plateID}")
    public SR getCarByPlate(@PathVariable String plateID){
        Car carByPlate = carService.getCarByPlate(plateID);
        return SR.ok().data(carByPlate);
    }

    @ApiOperation("根据车牌和品牌查找车辆")
    @GetMapping("/plate")
    public SR getCarByPlate(String carPlate,String carBrand){
        List<Car> list = carService.getCarByPlateAndBrand(carPlate, carBrand);
        return SR.ok().data(list);
    }


    @ApiOperation("上传车辆图片")
    @PostMapping("/upload")
    @ApiImplicitParams({@ApiImplicitParam(name = "file",value = "文件",dataType = "file")})
    public SR uploadImg(MultipartFile file){
        System.out.println(file+"****++++++****");
        try {
            System.out.println(file);
            String url = OSSUtils.uploadFile(UUIDUtils.getUUID().toString(), "car/", file);
            return SR.ok().data(url);
        } catch (Exception e) {
            e.printStackTrace();
            return SR.error();
        }
    }

}
